<?php
namespace Tbf\Component\FileSystem\Tests;
use Tbf\Component\FileSystem\FileSystemFunction;

class MkdirTest extends FileSystemTestCase
{
    protected $base_dir = null;
    public function setUp()
    {
        $this->base_dir = __DIR__.'/Fixture';
        FileSystemFunction::delete($this->base_dir);
    }
    public function tearDown()
    {
        FileSystemFunction::delete($this->base_dir);
    }
    public function testNoDirectory()
    {
        FileSystemFunction::mkdir($this->base_dir.'/d1');
        $this->assertTrue(FileSystemFunction::isPlainDir($this->base_dir.'/d1'));
    }
    public function testOverrideFile()
    {
        FileSystemFunction::putContent($this->base_dir.'/f1','file');
        FileSystemFunction::mkdir($this->base_dir.'/f1');
        $this->assertTrue(FileSystemFunction::isPlainDir($this->base_dir.'/f1'));
    }
    public function testOverrideLinkFile()
    {
        FileSystemFunction::putContent($this->base_dir.'/f1','file');
        FileSystemFunction::symlink($this->base_dir.'/f1',$this->base_dir.'/l1');
        $this->assertFalse(FileSystemFunction::isPlainDir($this->base_dir.'/l1'));
        FileSystemFunction::mkdir($this->base_dir.'/l1');
        $this->assertTrue(FileSystemFunction::isPlainDir($this->base_dir.'/l1'));
    }
    public function testNotOverrideLinkDir()
    {
        FileSystemFunction::putContent($this->base_dir.'/f1/f2','file');
        FileSystemFunction::symlink($this->base_dir.'/f1',$this->base_dir.'/l1');
        $this->assertTrue(is_link($this->base_dir.'/l1'));
        $this->assertTrue(is_dir($this->base_dir.'/l1'));

        FileSystemFunction::mkdir($this->base_dir.'/l1');
        $this->assertTrue(is_link($this->base_dir.'/l1'));
        $this->assertTrue(is_dir($this->base_dir.'/l1'));
    }
    public function testNotOverrideFile()
    {
        FileSystemFunction::putContent($this->base_dir.'/f1','file');
        FileSystemFunction::mkdir($this->base_dir.'/f1',false);
        $this->assertFalse(FileSystemFunction::isPlainDir($this->base_dir.'/f1'));
    }
    public function testWithDirExist()
    {
        FileSystemFunction::mkdir($this->base_dir.'/d1');
        $this->assertTrue(FileSystemFunction::isPlainDir($this->base_dir.'/d1'));
        FileSystemFunction::mkdir($this->base_dir.'/d1');
        $this->assertTrue(FileSystemFunction::isPlainDir($this->base_dir.'/d1'));
    }
    public function testNotPermission()
    {
        $this->markTestIncomplete('测试环境无法搭建....');
        FileSystemFunction::mkdir($this->base_dir,true);
        FileSystemFunction::mkdir($this->base_dir.'/d1',true,0);
        $this->assertTrue(FileSystemFunction::isPlainDir($this->base_dir.'/d1'));
        FileSystemFunction::mkdir($this->base_dir.'/d1/d2');
        $this->assertTrue(FileSystemFunction::isPlainDir($this->base_dir.'/d1/d2'));
    }
}
